package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lixu
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {
    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Autowired
    private DtsUserAccountMapper dtsUserAccountMapper;
    @Autowired
    private DtsAccountTraceMapper dtsAccountTraceMapper;
    @Autowired
    private DtsAddressMapper dtsAddressMapper;
    @Autowired
    private DtsCollectMapper dtsCollectMapper;
    @Autowired
    private DtsFootprintMapper dtsFootprintMapper;
    @Autowired
    private DtsSearchHistoryMapper dtsSearchHistoryMapper;
    @Autowired
    private DtsFeedbackMapper dtsFeedbackMapper;
    @Override
    public List<DayStatis> findDayStatis(Integer statisDayNum) {
        return dtsUserMapper.findDayStatis(statisDayNum);
    }

    @Override
    public IPage<DtsUser> findUserListByUsernameAndMobile(Integer page, Integer limit, String sort, String order, String username,String mobile) {
        return dtsUserMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsUser>()
                .like(!StringUtils.isEmpty(username),"username",username)
                .like(!StringUtils.isEmpty(mobile),"mobile",mobile)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public void modifyUserLevelAndRate(Integer userId, Integer settlementRate) {
        //修改用户层级和状态
        DtsUser dtsUser = new DtsUser();
        dtsUser.setUserLevel(2);
        dtsUser.setStatus(0);
        dtsUserMapper.update(dtsUser,new UpdateWrapper<DtsUser>().eq("id",userId));
        //修改佣金
        DtsUserAccount dtsUserAccount = new DtsUserAccount();
        dtsUserAccount.setSettlementRate(settlementRate);
        dtsUserAccountMapper.update(dtsUserAccount,new UpdateWrapper<DtsUserAccount>().eq("user_id",userId));

    }

    @Override
    public DtsUserAccount findDetailApproveById(Integer id) {
        return dtsUserAccountMapper.selectOne(new QueryWrapper<DtsUserAccount>().eq("user_id",id));
    }

    @Override
    public IPage<DtsAccountTrace> findBrokerageListByQuery(Integer page, Integer limit, String sort,
                                                           String order, String username,Integer mobile,Integer[] statusArray) {
        //根据用户名查询id集合
        List<Integer> userIds = dtsUserMapper.selectList(new QueryWrapper<DtsUser>()
                .like(!StringUtils.isEmpty(username), "username", username))
                .stream().map(DtsUser::getId).collect(Collectors.toList());
        return dtsAccountTraceMapper.selectPage(new Page<>(page, limit), new QueryWrapper<DtsAccountTrace>()
                .in(!StringUtils.isEmpty(userIds),"user_id", userIds)
                .in(!StringUtils.isEmpty(statusArray),"status", statusArray)
                .like(!StringUtils.isEmpty(mobile),"mobile",mobile)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public IPage<DtsAddress> findAddressListByQuery(Integer page, Integer limit, String sort, String order, String name,Integer userId) {
        return dtsAddressMapper.selectPage(new Page<>(page, limit), new QueryWrapper<DtsAddress>()
                .eq(!StringUtils.isEmpty(userId), "user_id", userId)
                .like(!StringUtils.isEmpty(name), "name", name)
                .orderBy(!StringUtils.isEmpty(sort), "ASC".equalsIgnoreCase(order), sort));
    }

    @Override
    public IPage<DtsCollect> findCollectListByQuery(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        return dtsCollectMapper.selectPage(new Page<>(page, limit), new QueryWrapper<DtsCollect>()
                .eq(!StringUtils.isEmpty(userId), "user_id", userId)
                .eq(!StringUtils.isEmpty(valueId), "value_id", valueId)
                .orderBy(!StringUtils.isEmpty(sort), "ASC".equalsIgnoreCase(order), sort));
    }

    @Override
    public IPage<DtsFootprint> findFootprintListByQuery(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        return dtsFootprintMapper.selectPage(new Page<>(page, limit), new QueryWrapper<DtsFootprint>()
                .eq(!StringUtils.isEmpty(userId), "user_id", userId)
                .eq(!StringUtils.isEmpty(goodsId), "goods_id", goodsId)
                .orderBy(!StringUtils.isEmpty(sort), "ASC".equalsIgnoreCase(order), sort));
    }

    @Override
    public void modifyBrokerageByUserId(Integer id, Integer status, String traceMsg) {
        DtsAccountTrace dtsAccountTrace = new DtsAccountTrace();
        dtsAccountTrace.setStatus(status);
        dtsAccountTrace.setTraceMsg(traceMsg);
        dtsAccountTraceMapper.update(dtsAccountTrace,new UpdateWrapper<DtsAccountTrace>().eq("id",id));
    }

    @Override
    public IPage<DtsSearchHistory> findHistoryListByQuery(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        return dtsSearchHistoryMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsSearchHistory>()
                .select("id","user_id","keyword","`from`","add_time","update_time","deleted")
                .eq(!StringUtils.isEmpty(userId),"user_id",userId)
                .like(!StringUtils.isEmpty(keyword),"keyword",keyword)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public IPage<DtsFeedback> findFeedbackListByQuery(Integer page, Integer limit, String sort, String order, String username, Integer id) {
        return dtsFeedbackMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsFeedback>()
                .eq(!StringUtils.isEmpty(id),"id",id)
                .like(!StringUtils.isEmpty(username),"username",username)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }


}
